<template>
  <div class="pb50">
    <el-form
      size="small"
      ref="form"
      :model="form"
      label-width="150px"
      v-if="!loading"
    >
      <!--商品信息-->
      <Info></Info>

      <!--规格-->
      <Spec></Spec>

      <!--其它-->
      <Other></Other>

      <!--提交-->
    </el-form>

    <div class="common-button-wrapper">
      <el-button size="small" type="info" @click="cancelFunc">取消</el-button>
      <el-button
        size="small"
        type="primary"
        @click="onSubmit"
        :loading="loading"
        >提交</el-button
      >
    </div>
  </div>
</template>
<script>
import PointProductApi from "@/api/pointproduct.js";
import Info from "./part/Info.vue";
import Spec from "./part/Spec.vue";
import Other from "./part/Other.vue";
import { formatModel } from "@/utils/base.js";
export default {
  components: {
    /*产品基本信息*/
    Info,
    /*规格*/
    Spec,
    /*其它*/
    Other,
  },
  data() {
    return {
      /*是否正在加载*/
      loading: true,
      /*表单*/
      form: {
        /*商品ID*/
        pointProductId: 0,
        /*商品状态*/
        status: 10,
        /*商品名称*/
        productName: "",
        /*图片*/
        productImage: "",
        /*排序*/
        sort: 100,
        /*规格*/
        productSkuId: 0,
        /*限购数量*/
        limitNum: 1,
        /*商品表格*/
        tableData: [],
      },
    };
  },
  provide: function () {
    return {
      form: this.form,
      type: "add",
    };
  },
  created() {
    if (this.$route.query.pointProductId != null) {
      this.form.point_product_id = this.$route.query.pointProductId;
      this.getData();
    }
  },
  methods: {
    /*获取商品*/
    getData() {
      let self = this;
      PointProductApi.getPointProduct(
        {
          pointProductId: self.form.point_product_id,
        },
        true
      ).then((res) => {
        self.form.image = [];
        self.form.productName = res.data.productName;
        self.form.filePath = res.data.imagePath;
        self.form.productId = res.data.productId;
        self.form.limitNum = res.data.limitNum;
        self.form.status = res.data.status;
        self.form.sort = res.data.sort;
        self.form.stock = res.data.stock;
        res.data.sku.forEach((item) => {
          item.productName = self.form.productName;
          item.specName = item.productAttr;
          item.specType = res.data.specType;
        });
        self.form.tableData = res.data.sku;
        self.loading = false;
      });
    },

    /*提交表单*/
    onSubmit() {
      let self = this;
      self.$refs.form.validate((valid) => {
        if (valid) {
          let params = {};
          params.productId = self.form.productId;
          params.status = self.form.status;
          params.sort = self.form.sort;
          params.limitNum = self.form.limitNum > -1 ? self.form.limitNum : 1;
          //params.limitNum = self.form.limitNum;
          params.pointProductId = self.form.point_product_id;
          params.productId = self.form.productId;
          params.specList = self.tableFormet(self.form.tableData);
          self.loading = true;
          PointProductApi.editPointProduct(params, true)
            .then((data) => {
              self.loading = false;
              ElMessage({
                message: "添加成功",
                type: "success",
              });
              self.cancelFunc();
            })
            .catch((error) => {
              self.loading = false;
            });
        }
      });
    },

    /*表格数据格式化*/
    tableFormet(list) {
      list.forEach((item) => {
        item.productAttr = item.specName;
      });
      return list;
    },

    /*取消*/
    cancelFunc() {
      this.$router.back(-1);
    },
  },
};
</script>
<style>
.edit_container {
  font-family: "Avenir", Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
}

.ql-editor {
  height: 400px;
}
</style>
